package cc.customer.web.util;

import cc.customer.bigEmport.damain.BigEmp;
import cc.customer.common.domain.ResponseBo;
import cc.customer.common.utils.DateUtil;
import cc.customer.common.utils.Utils;
import cc.customer.common.utils.poi.POIUtils;
import cc.customer.myCustomer.damain.MyCustomer;
import cc.customer.system.domain.MyUser;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;

public class PersonExcel {

	private final static Logger logger = LoggerFactory.getLogger(PersonExcel.class);

	/**校验个人资源的导入的数据的正确性**/
	public static ResponseBo PersonValiableExport(String fileUrlExport){
		Boolean isCheckExport = false;
		int num = 0;
		String msg = "验证成功";
		try {
			File file = new File(fileUrlExport);
			Workbook rwb= new XSSFWorkbook(new FileInputStream(file));
			Sheet sheet = rwb.getSheetAt(0);
			/**获取所有的行**/
			int rows = sheet.getLastRowNum();
			if (rows >500 ){return ResponseBo.error("一次性导入数据过多,最大导入 500条");}
			if (rows<=0){ return ResponseBo.error("导入数据为空"); }
			for (int i = 1; i <= rows; i++) {
				num++;Row row = sheet.getRow(i);
				//获取客户名称 客户名称不能为空
				Cell bpName = row.getCell(0);
				if (bpName == null){
					isCheckExport = true;msg = "客户名称为空";
					break;
				}
			}
			//验证是不是存在不是手机号或者 手机号不正确的数据
			if (isCheckExport){
				return ResponseBo.error("验证失败：第 "+num+" 条数据不正确,原因:"+msg);
			}
		} catch (Exception e) {
			logger.error("获取的个人资源导入失败");
		}
		return ResponseBo.ok("验证成功");
	}

	/**读取我的客户导入的信息**/
	public static List<MyCustomer> createExcelExport(String fileUrl, MyUser user) {
		List<MyCustomer> list = new ArrayList<>();
		try {
			File file = new File(fileUrl);
			Workbook rwb= new XSSFWorkbook(new FileInputStream(file));
			Sheet sheet = rwb.getSheetAt(0);
			int rows = sheet.getLastRowNum();//获取所有得行
			for (int i = 1; i <= rows; i++) {
				MyCustomer myCustomer = new MyCustomer();
				Row row = sheet.getRow(i);
				//获取客户名称
				Cell bpName = row.getCell(0);
				if (bpName!=null){ myCustomer.setMyCusName(bpName.getStringCellValue()); }
				//获取客户性别
				Cell bpSex = row.getCell(1);
				if(bpSex!=null){ myCustomer.setMyCusSex(bpSex.getStringCellValue()); }
				//获取手机号
				Cell bphone = row.getCell(2);
				if(bphone!=null){
					String ph = POIUtils.getCellValue(bphone);
					if (!StringUtils.isEmpty(ph)){myCustomer.setMyCusMobile(ph);}
				}
				//获取qq号
				Cell qq = row.getCell(3);
				if (qq != null){
					String qqvalue  = POIUtils.getCellValue(qq);
					if (!StringUtils.isEmpty(qqvalue)){myCustomer.setMyCusQq(qqvalue);}
				}
				//获取微信号
				Cell wechat = row.getCell(4);
				if (wechat !=null){
					String we = POIUtils.getCellValue(wechat);
					if (!StringUtils.isEmpty(we)){myCustomer.setMyCusWechat(we);}
				}
				//获取邮箱
				Cell email = row.getCell(5);
				if (email !=null){
					String emol = POIUtils.getCellValue(email);
					if (!StringUtils.isEmpty(emol)){myCustomer.setMyCusEmail(emol);}
				}
				//获取地域
				Cell zone = row.getCell(6);
				if (zone !=null){
					String zoneValue = zone.getStringCellValue();
					if (!StringUtils.isEmpty(zoneValue)){myCustomer.setMyCusCity(zoneValue);}
				}
				String time = DateUtil.getCurrentDate("yyyy-MM-dd HH:mm:ss");
				//增加其他辅助信息
				myCustomer.setMyCusUuid(Utils.getUUID());
				myCustomer.setMyCusCreator(user.getUserUuid());
				myCustomer.setMyCusReceiver(user.getUserUuid());
				myCustomer.setMyCusIsdelete("0");myCustomer.setMyCusStage("0");
				myCustomer.setMyCusCreatetime(time);
				myCustomer.setMyRecentTime(time); myCustomer.setMuRecent(user.getNickname()+"导入个人资源");
				myCustomer.setMyCusDept(user.getDeptId()+"");
				list.add(myCustomer);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("导入个人资源失败:"+e.getMessage());
		}
		return list;
	}
}
